from django.forms import fields
from rest_framework import serializers

from .models import ScenicAreaType, ScenicArea, ScenicAreaMap,\
    Track, RoadType, Road, IconLegend,  RoadScenics, Multimap, CronTask
from . import models
from . import  utils

class IconLegendSerializer(serializers.ModelSerializer):
    icon = serializers.SerializerMethodField('get_icon')
    
    def get_icon(self, obj):
        return None if obj.icon is None else str(obj.icon.url)
    class Meta:
        model = IconLegend
        fields = ['id', 'name', 'icon', 'icon_width', 'icon_height']

class ScenicAreaTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = ScenicAreaType
        fields = ['id', 'name', 'show', ]


class SimpleScenicAreaSerializer(serializers.ModelSerializer):
    class Meta:
        model = ScenicArea
        fields = ['id', 'name', 'popup_width', 'popup_content', 'show_footer', 'footer_position', 'footer_width', 
            'location', 'legend', 'show_contour_in_parent', 'contour_color', 'cma_color', 'mpoly', 'scenic_area_type', 'show_detail', 'show_map', 'show_nav', 'show_audio', 'bound_ratio']


class ScenicAreaMapSerializer(serializers.ModelSerializer):
    tile_dir = serializers.SerializerMethodField('get_tile_dir')
    img_width = serializers.SerializerMethodField('get_img_width')
    img_height = serializers.SerializerMethodField('get_img_height')

    def get_tile_dir(self, obj):
        return utils.get_tiles_uri(obj)

    def get_img_width(self, obj):
        return None if obj.map_image is None else obj.map_image.filer_image_file.width

    def get_img_height(self, obj):
        return None if obj.map_image is None else obj.map_image.filer_image_file.height

    class Meta:
        model = ScenicAreaMap
        fields = ['id', 'name', 'left_up', 'right_down', 'zoom', 'min_zoom', 'max_zoom', 'tile_dir', 'img_width', 'img_height']


class ScenicAreaSerializer(serializers.ModelSerializer):
    audio_file = serializers.SerializerMethodField('get_audio_file')
    children_scenic_area_type = serializers.SerializerMethodField('get_children_scenic_area_type')
    
    def get_audio_file(self, obj):
        return None if obj.audio_file is None else obj.audio_file.url

    def get_children_scenic_area_type(self, obj):
        scenic_types = []
        scenic_types.extend(ScenicAreaTypeSerializer(obj.children_scenic_area_type, many=True).data)
        return scenic_types
        

    class Meta:
        model = ScenicArea
        fields = ['id', 'name', 'popup_width', 'popup_content', 'show_footer', 'footer_position', 'footer_width',  'detail', 'location', 'mpoly', 
            'zoom', 'min_zoom', 'max_zoom' , 'show_contour_in_parent',  'show_contour', 'contour_color', 'cma_color',
            'legend', 'scenic_area_type', 'children_scenic_area_type', 'audio_file', 'show_detail', 'show_map', 'show_nav', 'show_audio', 'bound_ratio']

class TrackSerializer(serializers.ModelSerializer):
    class Meta:
        model = Track
        fields = ['id', 'points']

class SimpleRoadSerializer(serializers.ModelSerializer):
    class Meta:
        model = Road
        fields = ['id', 'name', 'road_type']


class RoadTypeDetailSerializer(serializers.ModelSerializer):
    roads = SimpleRoadSerializer(many=True, read_only=True)

    class Meta:
        model = RoadType
        fields = ['id', 'name', 'show', 'roads']
        depth = 1

class RoadSerializer(serializers.ModelSerializer):
    class Meta:
        model = Road
        fields = ['id', 'name', 'detail', 'show_route', 'route', 'contour_color', 'road_type']
        depth = 0


class MultimapSerializer(serializers.ModelSerializer):
    icon = serializers.SerializerMethodField('get_icon')
    
    def get_icon(self, obj):
        return None if obj.icon is None else str(obj.icon.url)
    class Meta:
        model = Multimap
        fields = ['id', 'name', 'icon', 'icon_width', 'icon_height', 'scenic_area', 'orderid']

class CronTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = CronTask
        fields = ['id', 'task_type', 'grade', 'detail', 'cron_task', 'scenic_area']
